var ReportBuilder = function() {
    var _self               = this;
    _self.reportData        = {};

    _self.prepareMainHeader = function(reportTitle){
        html = new StringBuilder();
        html.append("<div class='A4 p_1'>\n\
                        <div class='column w100'>\n\
                            <h1>"+reportTitle+"</h1>\n\
                        </div>\n\
                        <div class='column w25'>\n\
                            <table class='header-data w100'>\n\
                                <tr>\n\
                                    <td class='w25 left'>For</td>\n\
                                    <td class='w75' id='reportForDate'></td>\n\
                                </tr>\n\
                                <tr>\n\
                                    <td class='w25 left'>Entries</td>\n\
                                    <td class='w75'>---</td>\n\
                                </tr>\n\
                                <tr>\n\
                                    <td class='w25 left'>User</td>\n\
                                    <td class='w75' id='reportUser'></td>\n\
                                </tr>\n\
                                <tr>\n\
                                    <td class='w25 left'>Data</td>\n\
                                    <td class='w75' id='reportTime'></td>\n\
                                </tr>\n\
                                <tr>\n\
                                    <td class='w25 left'>Pages</td>\n\
                                    <td class='w75' id='reportPage'>1</td>\n\
                                </tr>\n\
                            </table>\n\
                        </div>\n\
                        <div class='column w75'>\n\
                            <div class='column w100 np' style='height: 95px;'>\n\
                                <hr>\n\
                                <div class='column np w25'>\n\
                                    <span>Notes</span>\n\
                                </div>\n\
                                <div class='column np w75 text-right'>\n\
                                    <span>Dr. Oetker Canada Ltd.</span>\n\
                                </div>\n\
                            </div>\n\
                        </div>\n\
                        <div class='column w100 np'>\n\
                            <div class='column w25'>\n\
                                <hr>\n\
                            </div>\n\
                            <div class='column w75'>\n\
                                <hr>    \n\
                            </div>\n\
                        </div>\n\
                        <div class='column w100 mainTable'>\n\
                        </div>\n\
                    </div>");
        return html.toString();
    }
    _self.loadDataToFirstPage = function(){
        $(".mainTable").append(html.toString());
    }
    _self.createPage = function(pageNumber,orientation){
        var html = new StringBuilder();
        html.append("<div class='A4 p_"+pageNumber+"'></div>");
        
        $("#reportHolder").append(html.toString());
        $(".A4.p_" + pageNumber).addClass(orientation);
    }
    _self.updateMainHeader = function(params,pages){
        $("#reportUser").html(params.user);
        $("#reportForDate").html(params.date);
        var date = new Date();
        var ampm = (date.getHours() >= 12) ? "PM" : "AM";
        $("#reportTime").html(date.getDate() + "/" + (date.getMonth() + 1) + "/" + (date.getYear() + 1900) + " " + date.getHours() + ":" + date.getMinutes() + " " + ampm);
        $("#reportPage").html(pages);
    };
    _self.callBack = function(callback){
        if (callback) {
            callback();
        }
    };

    _self.buildMxerDailyReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = _self.prepareMainHeader("mixing - daily report");


        _self.reportData.resultHeader = "<tr class='heading'>\n\
                                            <td class='w10'>Recipe</td>\n\
                                            <td class='w65 text-left'>Description</td>\n\
                                            <td class='w5'>Mixer</td>\n\
                                            <td class='w5'>Batch size</td>\n\
                                            <td class='w5'>Dumping</td>\n\
                                            <td class='w5'>Draining</td>\n\
                                            <td class='w5'>Total [min]</td>\n\
                                        </tr>";
        
        _self.reportData.rowDefinition = "<tr>\n\
                                            <td class='w10'>#Recipe#</td>\n\
                                            <td class='w65 text-left'>#Description#</td>\n\
                                            <td class='w5'>#Mixer#</td>\n\
                                            <td class='w5'>#Batch#</td>\n\
                                            <td class='w5'>#Dumping#</td>\n\
                                            <td class='w5'>#Draining#</td>\n\
                                            <td class='w5'>#Total#</td>\n\
                                        </tr>";

        _self.reportData.rowDefinitionTot = "<tr style = 'font-weight: bold;'>\n\
                                <td class='w10'>#Recipe#</td>\n\
                                <td class='w65 text-left'>#Description#</td>\n\
                                <td class='w5'>#Mixer#</td>\n\
                                <td class='w5'>#Batch#</td>\n\
                                <td class='w5'>#Dumping#</td>\n\
                                <td class='w5'>#Draining#</td>\n\
                                <td class='w5'>#Total#</td>\n\
                            </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {

            var count = result.length;
            var html = new StringBuilder();
            var perPage = 20;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "portrait";

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;

                if(resultTracker == perPage){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){
                        htmlHeader.append("<div class='w20 strong-label'>Mixing</div>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").html(htmlHeader.toString());
                        $(".A4.p_" + pages).addClass(orientation);
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<div class='w20 strong-label'>Mixing</div>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }

                    html.clear();
                    pages = pages + 1;

                }

                resultTracker = resultTracker + 1;

                var Recipe      = (result[i].Recipe         !== undefined)  ? result[i].Recipe:         "";
                var Description = (result[i].Description    !== undefined)  ? result[i].Description:    "";
                var Mixer       = (result[i].Mixer          !== undefined)  ? result[i].Mixer:          "";
                var Batch       = (result[i].BatchSize      !== undefined)  ? result[i].BatchSize:      "";
                var Dumping     = (result[i].Dumping        !== undefined)  ? result[i].Dumping:        "";
                var Draining    = (result[i].Draining       !== undefined)  ? result[i].Draining:       "";
                var Total       = (result[i].Total          !== undefined)  ? result[i].Total:          "";

                if(result[i].Item == "total"){
                    html.append(
                    _self.reportData.rowDefinitionTot.replace("#Recipe#"         , "Total"        )
                    .replace("#Description#"    , Description   )
                    .replace("#Mixer#"          , Mixer         )
                    .replace("#Batch#"          , Batch         )
                    .replace("#Dumping#"        , Dumping       )
                    .replace("#Draining#"       , Draining      )
                    .replace("#Total#"          , Total         ));
                }
                else{
                    html.append(
                        _self.reportData.rowDefinition.replace("#Recipe#"         , Recipe        )
                        .replace("#Description#"    , Description   )
                        .replace("#Mixer#"          , Mixer         )
                        .replace("#Batch#"          , Batch         )
                        .replace("#Dumping#"        , Dumping       )
                        .replace("#Draining#"       , Draining      )
                        .replace("#Total#"          , Total         ));
                }

            }

            if(pages === 1){
                var htmlHeader = new StringBuilder();
                htmlHeader.append("<div class='column w100 mainTable'>");
                htmlHeader.append("<div class='w20 strong-label'>Mixing</div>");
                htmlHeader.append("<table class='w100'>");
                htmlHeader.append(_self.reportData.resultHeader);
                htmlHeader.append(html.toString());
                htmlHeader.append("</table>");
                htmlHeader.append("</div>");
                $("#reportHolder").html(_self.reportData.mainHeader);
                $("#reportHolder .mainTable").html(htmlHeader.toString());
                $(".A4.p_1").addClass(orientation);
                _self.updateMainHeader(params,1);
            }
            else
                _self.updateMainHeader(params,(pages-1));
            
            _self.reportData = {};
            _self.callBack(callback);
        });
    };
    _self.buildMxerMaterialsReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = _self.prepareMainHeader("mixing - material times");

        _self.reportData.resultHeader = "<tr class='heading'>\n\
                                            <td class='w10'>Recipe</td>\n\
                                            <td class='w55'>Description</td>\n\
                                            <td class='w5'>Avg. Pick. [min]</td>\n\
                                            <td class='w5'>Avg. Direct. [min]</td>\n\
                                            <td class='w5'>Sub Tot. [min]</td>\n\
                                            <td class='w5'>Overhead [min]</td>\n\
                                            <td class='w5'>Mixing [min]</td>\n\
                                            <td class='w5'>Total [min]</td>\n\
                                            <td class='w5'>Entries</td>\n\
                                        </tr>";
        
        _self.reportData.rowDefinition = "<tr>\n\
                                            <td class='w10'>#Recipe#</td>\n\
                                            <td class='w55 text-left'>#Description#</td>\n\
                                            <td class='w5'>#AvgPick#</td>\n\
                                            <td class='w5'>#AvgDirect#</td>\n\
                                            <td class='w5'>#SubTot#</td>\n\
                                            <td class='w5'>#Overhead#</td>\n\
                                            <td class='w5'>#MixingTime#</td>\n\
                                            <td class='w5'>#Total#</td>\n\
                                            <td class='w5'>#Entries#</td>\n\
                                        </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {

            var count = result.length;
            var html = new StringBuilder();
            var perPage = 20;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "landscape";

            var destination = "";
            var previousDestination = "";

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;

                destination = result[i].Destination;

                if(previousDestination == 0)
                    previousDestination = destination;

                if(destination != previousDestination || resultTracker == perPage){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){
                        htmlHeader.append("<div class='w20 strong-label'>Line "+previousDestination+"</div>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").html(htmlHeader.toString());
                        $(".A4.p_" + pages).addClass(orientation);
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<div class='w20 strong-label'>Line "+previousDestination+"</div>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }

                    html.clear();
                    pages = pages + 1;

                }
                previousDestination = destination;

                resultTracker = resultTracker + 1;

                var Description     = (result[i].Description    !== undefined)  ? result[i].Description:    "";
                var Recipe          = (result[i].Recipe         !== undefined)  ? result[i].Recipe:         "";
                var AvgPick         = (result[i].AvgPick        !== undefined)  ? result[i].AvgPick:        "";
                var AvgDirect       = (result[i].AvgDirect      !== undefined)  ? result[i].AvgDirect:      "";
                var SubTot          = (result[i].SubTot         !== undefined)  ? result[i].SubTot:         "";
                var Overhead        = (result[i].Overhead       !== undefined)  ? result[i].Overhead:       "";
                var MixingTime      = (result[i].MixingTime     !== undefined)  ? result[i].MixingTime:     "";
                var Total           = (result[i].Total          !== undefined)  ? result[i].Total:          "";
                var Entries         = (result[i].Entries        !== undefined)  ? result[i].Entries:        "";


                html.append(
                    _self.reportData.rowDefinition.replace("#Description#"    , Description   )
                    .replace("#Recipe#"         , Recipe        )
                    .replace("#AvgPick#"        , AvgPick       )
                    .replace("#AvgDirect#"      , AvgDirect     )
                    .replace("#SubTot#"         , SubTot        )
                    .replace("#Overhead#"       , Overhead      )
                    .replace("#MixingTime#"     , MixingTime    )
                    .replace("#Total#"          , Total         )
                    .replace("#Entries#"        , Entries       ));

            }

            if(pages === 1){
                var htmlHeader = new StringBuilder();
                html.append("<div class='column w100 mainTable'>");
                html.append("<div class='w20 strong-label'>Line "+previousDestination+"</div>");
                html.append("<table class='w100'>");
                html.append(_self.reportData.resultHeader);
                html.append(html.toString());
                html.append("</table>");
                html.append("</div>");
                $("#reportHolder").html(_self.reportData.mainHeader);
                $(".mainTable").html(html.toString());
            }

            _self.updateMainHeader(params,(pages-1));
            _self.reportData = {};
            _self.callBack(callback);
        });
    };
    _self.buildMixerOverheadReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = _self.prepareMainHeader("packaging - output");

        _self.reportData.resultHeader = "<tr class='heading'>\n\
                                            <td class='w15'>Date</td>\n\
                                            <td class='w10'>Shift</td>\n\
                                            <td class='w15'>Available <br>[min]</td>\n\
                                            <td class='w15'>Dumping <br>[min]</td>\n\
                                            <td class='w15'>Draining <br>[min]</td>\n\
                                            <td class='w15'>Picking <br>[min]</td>\n\
                                            <td class='w15'>Overhead <br>[min]</td>\n\
                                        </tr>";
        _self.reportData.rowDefinition = "<tr>\n\
                                            <td class='w15'>#Date#</td>\n\
                                            <td class='w10'>#Shift#</td>\n\
                                            <td class='w15'>#Available#</td>\n\
                                            <td class='w15'>#Dumping#</td>\n\
                                            <td class='w15'>#Draining#</td>\n\
                                            <td class='w15'>#Picking#</td>\n\
                                            <td class='w15'>#Overhead#</td>\n\
                                        </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {
            var count = result.length;
            var html = new StringBuilder();
            var perPage = 20;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "portrait";

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;
                
                resultTracker = resultTracker + 1;
                var Date       = (result[i].Date        !== undefined)  ? result[i].Date        : "";
                var Shift      = (result[i].Shift       !== undefined)  ? result[i].Shift       : "";
                var Available  = (result[i].Available   !== undefined)  ? result[i].Available   : "";
                var Dumping    = (result[i].Dumping     !== undefined)  ? result[i].Dumping     : "";
                var Draining   = (result[i].Draining    !== undefined)  ? result[i].Draining    : "";
                var Picking    = (result[i].Picking     !== undefined)  ? result[i].Picking     : "";
                var Overhead   = (result[i].Overhead    !== undefined)  ? result[i].Overhead    : "";


                html.append(
                    _self.reportData.rowDefinition.replace("#Date#"      ,Date        )
                    .replace("#Shift#"     ,Shift       )
                    .replace("#Available#" ,Available   )
                    .replace("#Dumping#"   ,Dumping     )
                    .replace("#Draining#"  ,Draining    )
                    .replace("#Picking#"   ,Picking     )
                    .replace("#Overhead#"  ,Overhead    ));

                if(resultTracker == perPage){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){                        
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").html(htmlHeader.toString());
                        $(".A4.p_" + pages).addClass(orientation);
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }

                    html.clear();
                    pages = pages + 1;

                }
            }

            if(pages === 1){
                var htmlHeader = new StringBuilder();
              
                htmlHeader.append("<table class='w100'>");
                htmlHeader.append(_self.reportData.resultHeader);
                htmlHeader.append(html.toString());
                htmlHeader.append("</table>");
                $("#reportHolder").html(_self.reportData.mainHeader);
                $(".mainTable").html(htmlHeader.toString());
                $(".A4.p_" + pages).addClass(orientation);
            }

            _self.updateMainHeader(params,(pages-1));
            _self.reportData = {};
            _self.callBack(callback);
        });
    };

    _self.buildPackagingDailyReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = _self.prepareMainHeader("packaging daily report");

        _self.reportData.resultHeader = "<tr class='heading'>\n\
                                            <td class='w5'><div class='rotate'>Line</div></td>\n\
                                            <td class='w5'><div class='rotate'>Speed</div></td>\n\
                                            <td class='w5'><div class='rotate'>Time</div></td>\n\
                                            <td class='w5'><label class='pouches'>Pouches</label>Total</td>\n\
                                            <td class='w5'>Actual</td>\n\
                                            <td class='w5'>Item#</td>\n\
                                            <td class='w40 text-left'>Description</td>\n\
                                            <td class='w5'>P.E.%</td>\n\
                                            <td class='w25'>Comment</td>\n\
                                        </tr>";
        
         _self.reportData.rowDefinition = "<tr>\n\
                                            <td class='w5'>#line#</td>\n\
                                            <td class='w5'>#speed#</td>\n\
                                            <td class='w5'>#time#</td>\n\
                                            <td class='w5'>#pouchesTotal#</td>\n\
                                            <td class='w5'>#pouchesActual#</td>\n\
                                            <td class='w5'>#item#</td>\n\
                                            <td class='w40 text-left'>#description#</td>\n\
                                            <td class='w5'>#pe#</td>\n\
                                            <td class='w25'>#comment#</td>\n\
                                        </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {
            var count = result.length;
            var html = new StringBuilder();
            var perPage = 20;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "landscape";

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;

                if(resultTracker == perPage){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){                        
                        htmlHeader.append("<table class='w300'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").html(htmlHeader.toString());
                        $(".A4.p_" + pages).addClass(orientation);
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }

                    html.clear();
                    pages = pages + 1;

                }

                resultTracker = resultTracker + 1;
                var line                    = (result[i].line                 !== undefined) ? result[i].line                   : "";
                var speed                   = (result[i].speed                !== undefined) ? result[i].speed                  : "";
                var time                    = (result[i].time                 !== undefined) ? result[i].time                   : "";
                var pouchesTotal            = (result[i].pouchesTotal         !== undefined) ? result[i].pouchesTotal           : "";
                var pouchesActual           = (result[i].pouchesActual        !== undefined) ? result[i].pouchesActual          : "";
                var productionEfficiency    = (result[i].productionEfficiency !== undefined) ? result[i].productionEfficiency   : "";
                var item                    = (result[i].item                 !== undefined) ? result[i].item                   : "";
                var description             = (result[i].description          !== undefined) ? result[i].description            : "";
                var comment                 = (result[i].comment              !== undefined) ? result[i].comment                 : "";


                html.append(
                    _self.reportData.rowDefinition.replace("#line#", line)
                        .replace("#speed#", speed)
                        .replace("#time#", time)
                        .replace("#pouchesTotal#", pouchesTotal)
                        .replace("#pouchesActual#", pouchesActual)
                        .replace("#pe#", productionEfficiency)
                        .replace("#item#", item)
                        .replace("#description#", description)
                        .replace("#comment#", comment));
            }

            if(pages === 1){
                var htmlHeader = new StringBuilder();
              
                htmlHeader.append("<table class='w300'>");
                htmlHeader.append(_self.reportData.resultHeader);
                htmlHeader.append(html.toString());
                htmlHeader.append("</table>");
                $("#reportHolder").html(_self.reportData.mainHeader);
                $(".mainTable").html(htmlHeader.toString());
                $(".A4.p_" + pages).addClass(orientation);
            }

            _self.updateMainHeader(params,(pages-1));
            _self.reportData = {};
            _self.callBack(callback);
        });
    };
    _self.buildPackagingOutputReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = _self.prepareMainHeader("packaging - output");

        _self.reportData.resultHeader = "<tr class='heading'>\n\
                                            <td class='w5'><div class='rotate'>Line</div></td>\n\
                                            <td class='w5'><div class='rotate'>Shift</div></td>\n\
                                            <td class='w5'><div class='rotate'>Speed</div></td>\n\
                                            <td class='w5'><div class='rotate'>Time</div></td>\n\
                                            <td class='w5'>#Item</td>\n\
                                            <td class='w30 text-left inline'>Description</td>\n\
                                            <td class='w5'>Actual</td>\n\
                                            <td class='w5'>Total</td>\n\
                                            <td class='w5'>W%</td>\n\
                                            <td class='w5'>Actual</td>\n\
                                            <td class='w5'>Total</td>\n\
                                            <td class='w5'>W%</td>\n\
                                            <td class='w5'>Actual</td>\n\
                                            <td class='w5'>Total</td>\n\
                                            <td class='w5'>W%</td>\n\
                                        </tr>";
        
        _self.reportData.rowDefinition = "<tr>\n\
                                            <td class='w5'>#line#</td>\n\
                                            <td class='w5'>#shift#</td>\n\
                                            <td class='w5'>#speed#</td>\n\
                                            <td class='w5'>#time#</td>\n\
                                            <td class='w5'>#item#</td>\n\
                                            <td class='w30 text-left inline'>#description#</td>\n\
                                            <td class='w5'>#pouchesActual#</td>\n\
                                            <td class='w5'>#pouchesTotal#</td>\n\
                                            <td class='w5'>#pouchesWaste#</td>\n\
                                            <td class='w5'>#casesActual#</td>\n\
                                            <td class='w5'>#casesTotal#</td>\n\
                                            <td class='w5'>#casesWaste#</td>\n\
                                            <td class='w5'>#webActual#</td>\n\
                                            <td class='w5'>#webTotal#</td>\n\
                                            <td class='w5'>#webWaste#</td>\n\
                                        </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {
            var count = result.length;
            var html = new StringBuilder();
            var perPage = 20;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "landscape";

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;
                
                resultTracker = resultTracker + 1;
                var line            = (result[i].line             !== undefined)  ? result[i].line             : "";
                var shift           = (result[i].shift            !== undefined)  ? result[i].shift            : "";
                var speed           = (result[i].speed            !== undefined)  ? result[i].speed            : "";
                var time            = (result[i].time             !== undefined)  ? result[i].time             : "";
                var item            = (result[i].item             !== undefined)  ? result[i].item             : "";
                var description     = (result[i].description      !== undefined)  ? result[i].description      : "";
                var pouchesActual   = (result[i].pouchesActual    !== undefined)  ? result[i].pouchesActual    : "";
                var pouchesTotal    = (result[i].pouchesTotal     !== undefined)  ? result[i].pouchesTotal     : "";
                var pouchesWaste    = (result[i].pouchesWaste     !== undefined)  ? result[i].pouchesWaste     : "";
                var casesActual     = (result[i].casesActual      !== undefined)  ? result[i].casesActual      : "";
                var casesTotal      = (result[i].casesTotal       !== undefined)  ? result[i].casesTotal       : "";
                var casesWaste      = (result[i].casesWaste       !== undefined)  ? result[i].casesWaste       : "";
                var webActual       = (result[i].webActual        !== undefined)  ? result[i].webActual        : "";
                var webTotal        = (result[i].webTotal         !== undefined)  ? result[i].webTotal         : "";
                var webWaste        = (result[i].webWaste         !== undefined)  ? result[i].webWaste         : "";


                html.append(
                    _self.reportData.rowDefinition.replace("#line#"        ,line          )
                    .replace("#shift#"          ,shift         )
                    .replace("#speed#"          ,speed         )
                    .replace("#time#"           ,time          )
                    .replace("#item#"           ,item          )
                    .replace("#description#"    ,description   )
                    .replace("#pouchesActual#"  ,pouchesActual )
                    .replace("#pouchesTotal#"   ,pouchesTotal  )
                    .replace("#pouchesWaste#"   ,pouchesWaste  )
                    .replace("#casesActual#"    ,casesActual   )
                    .replace("#casesTotal#"     ,casesTotal    )
                    .replace("#casesWaste#"     ,casesWaste    )
                    .replace("#webActual#"      ,webActual     )
                    .replace("#webTotal#"       ,webTotal      )
                    .replace("#webWaste#"       ,webWaste      )); 

                if(resultTracker == perPage){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){                        
                        htmlHeader.append("<table class='w300'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").html(htmlHeader.toString());
                        $(".A4.p_" + pages).addClass(orientation);
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }

                    html.clear();
                    pages = pages + 1;

                }
            }

            if(pages === 1){
                var htmlHeader = new StringBuilder();
              
                htmlHeader.append("<table class='w300'>");
                htmlHeader.append(_self.reportData.resultHeader);
                htmlHeader.append(html.toString());
                htmlHeader.append("</table>");
                $("#reportHolder").html(_self.reportData.mainHeader);
                $(".mainTable").html(htmlHeader.toString());
                $(".A4.p_" + pages).addClass(orientation);
            }

            _self.updateMainHeader(params,(pages-1));
            _self.reportData = {};
            _self.callBack(callback);
        });
    };
    _self.buildPackagingStatisticsReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = _self.prepareMainHeader("packaging - production statistics");

        _self.reportData.resultHeader = "<tr class='heading2'>\n\
                                            <td class='w16'>Speed</td>\n\
                                            <td class='w12' colspan='2'>Pouches</td>\n\
                                            <td class='w12' colspan='2'>Hours required</td>\n\
                                            <td class='w12' colspan='2'>PE %</td>\n\
                                            <td class='w12' colspan='2'>TPDT hrs</td>\n\
                                            <td class='w12' colspan='2'>TE %</td>\n\
                                            <td class='w12' colspan='2'>Usage %</td>\n\
                                            <td class='w12' colspan='2'>CO %</td>\n\
                                        </tr>\n\
                                        <tr class='heading2 no-bt'>\n\
                                            <td class='w16'></td>\n\
                                            <td class='w6'>P</td>\n\
                                            <td class='w6'>YTD</td>\n\
                                            <td class='w6'>P</td>\n\
                                            <td class='w6'>YTD</td>\n\
                                            <td class='w6'>P</td>\n\
                                            <td class='w6'>YTD</td>\n\
                                            <td class='w6'>P</td>\n\
                                            <td class='w6'>YTD</td>\n\
                                            <td class='w6'>P</td>\n\
                                            <td class='w6'>YTD</td>\n\
                                            <td class='w6'>P</td>\n\
                                            <td class='w6'>YTD</td>\n\
                                            <td class='w6'>P</td>\n\
                                            <td class='w6'>YTD</td>\n\
                                        </tr>";
        
        _self.reportData.rowDefinitionValue = "<tr>\n\
                                            <td class='w16'>#speed#</td>\n\
                                            <td class='w6'>#pouches#</td>\n\
                                            <td class='w6'>#pouches_YTD#</td>\n\
                                            <td class='w6'>#hours#</td>\n\
                                            <td class='w6'>#hours_YTD#</td>\n\
                                            <td class='w6'>#PE#</td>\n\
                                            <td class='w6'>#PE_YTD#</td>\n\
                                            <td class='w6'>#TPDT#</td>\n\
                                            <td class='w6'>#TPDT_YTD#</td>\n\
                                            <td class='w6'>#TE#</td>\n\
                                            <td class='w6'>#TE_YTD#</td>\n\
                                            <td class='w6'>#usage#</td>\n\
                                            <td class='w6'>#usage_YTD#</td>\n\
                                            <td class='w6'>#change_over#</td>\n\
                                            <td class='w6'>#change_over_YTD#</td>\n\
                                        </tr>";
        _self.reportData.rowDefinitionTotal = "<tr style='font-weight: bold;'>\n\
                                            <td class='w16'>#speed#</td>\n\
                                            <td class='w6'>#pouches#</td>\n\
                                            <td class='w6'>#pouches_YTD#</td>\n\
                                            <td class='w6'>#hours#</td>\n\
                                            <td class='w6'>#hours_YTD#</td>\n\
                                            <td class='w6'>#PE#</td>\n\
                                            <td class='w6'>#PE_YTD#</td>\n\
                                            <td class='w6'>#TPDT#</td>\n\
                                            <td class='w6'>#TPDT_YTD#</td>\n\
                                            <td class='w6'>#TE#</td>\n\
                                            <td class='w6'>#TE_YTD#</td>\n\
                                            <td class='w6'>#usage#</td>\n\
                                            <td class='w6'>#usage_YTD#</td>\n\
                                            <td class='w6'>#change_over#</td>\n\
                                            <td class='w6'>#change_over_YTD#</td>\n\
                                        </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {

            var count = result.length;
            var htmlValue = new StringBuilder();
            var htmlTotal = new StringBuilder();
            var perPage = 10;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "landscape";

            var line = 0;
            var previousLine = 0;

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;

                line = result[i].line_id;

                if(previousLine == 0)
                    previousLine = line;

                if(line != previousLine || resultTracker == perPage){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){
                        htmlHeader.append("<div class='w20 strong-label'>Line "+previousLine+"</div>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(htmlValue.toString());
                        htmlHeader.append(htmlTotal.toString());
                        htmlHeader.append("</table>");
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").html(htmlHeader.toString());
                        $(".A4.p_" + pages).addClass(orientation);
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<div class='w20 strong-label'>Line "+previousLine+"</div>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(htmlValue.toString());
                        htmlHeader.append(htmlTotal.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }

                    htmlValue.clear();
                    htmlTotal.clear();
                    pages = pages + 1;

                }
                previousLine = line;

                if(result[i].dataType === "value"){
                    resultTracker = resultTracker + 1;
                    var speed            = (result[i].speed           !== undefined)  ? result[i].speed            : "";
                    var pouches          = (result[i].pouches         !== undefined)  ? result[i].pouches          : "";
                    var pouches_YTD      = (result[i].pouches_YTD     !== undefined)  ? result[i].pouches_YTD      : "";
                    var hours            = (result[i].hours           !== undefined)  ? result[i].hours            : "";
                    var hours_YTD        = (result[i].hours_YTD       !== undefined)  ? result[i].hours_YTD        : "";
                    var PE               = (result[i].PE              !== undefined)  ? result[i].PE               : "";
                    var PE_YTD           = (result[i].PE_YTD          !== undefined)  ? result[i].PE_YTD           : "";
                    var TPDT             = (result[i].TPDT            !== undefined)  ? result[i].TPDT             : "";
                    var TPDT_YTD         = (result[i].TPDT_YTD        !== undefined)  ? result[i].TPDT_YTD         : "";
                    var TE               = (result[i].TE              !== undefined)  ? result[i].TE               : "";
                    var TE_YTD           = (result[i].TE_YTD          !== undefined)  ? result[i].TE_YTD           : "";
                    var usage            = (result[i].usage           !== undefined)  ? result[i].usage            : "";
                    var usage_YTD        = (result[i].usage_YTD       !== undefined)  ? result[i].usage_YTD        : "";
                    var change_over      = (result[i].change_over     !== undefined)  ? result[i].change_over      : "";
                    var change_over_YTD  = (result[i].change_over_YTD !== undefined)  ? result[i].change_over_YTD  : "";

                    htmlValue.append(
                        _self.reportData.rowDefinitionValue.replace("#speed#"          ,speed            )
                        .replace("#pouches#"           ,pouches          )
                        .replace("#pouches_YTD#"       ,pouches_YTD      )
                        .replace("#hours#"             ,hours            )
                        .replace("#hours_YTD#"         ,hours_YTD        )
                        .replace("#PE#"                ,PE               )
                        .replace("#PE_YTD#"            ,PE_YTD           )
                        .replace("#TPDT#"              ,TPDT             )
                        .replace("#TPDT_YTD#"          ,TPDT_YTD         )
                        .replace("#TE#"                ,TE               )
                        .replace("#TE_YTD#"            ,TE_YTD           )
                        .replace("#usage#"             ,usage            )
                        .replace("#usage_YTD#"         ,usage_YTD        )
                        .replace("#change_over#"       ,change_over      )
                        .replace("#change_over_YTD#"   ,change_over_YTD  ));
                }
                else if(result[i].dataType === "total"){
                    resultTracker = resultTracker + 1;
                    var speed            = "Total";
                    var pouches          = (result[i].pouches         !== undefined)  ? result[i].pouches          : "";
                    var pouches_YTD      = (result[i].pouches_YTD     !== undefined)  ? result[i].pouches_YTD      : "";
                    var hours            = (result[i].hours           !== undefined)  ? result[i].hours            : "";
                    var hours_YTD        = (result[i].hours_YTD       !== undefined)  ? result[i].hours_YTD        : "";
                    var PE               = (result[i].PE              !== undefined)  ? result[i].PE               : "";
                    var PE_YTD           = (result[i].PE_YTD          !== undefined)  ? result[i].PE_YTD           : "";
                    var TPDT             = (result[i].TPDT            !== undefined)  ? result[i].TPDT             : "";
                    var TPDT_YTD         = (result[i].TPDT_YTD        !== undefined)  ? result[i].TPDT_YTD         : "";
                    var TE               = (result[i].TE              !== undefined)  ? result[i].TE               : "";
                    var TE_YTD           = (result[i].TE_YTD          !== undefined)  ? result[i].TE_YTD           : "";
                    var usage            = (result[i].usage           !== undefined)  ? result[i].usage            : "";
                    var usage_YTD        = (result[i].usage_YTD       !== undefined)  ? result[i].usage_YTD        : "";
                    var change_over      = (result[i].change_over     !== undefined)  ? result[i].change_over      : "";
                    var change_over_YTD  = (result[i].change_over_YTD !== undefined)  ? result[i].change_over_YTD  : "";

                    htmlTotal.append(
                        _self.reportData.rowDefinitionTotal.replace("#speed#"          ,speed            )
                        .replace("#pouches#"           ,pouches          )
                        .replace("#pouches_YTD#"       ,pouches_YTD      )
                        .replace("#hours#"             ,hours            )
                        .replace("#hours_YTD#"         ,hours_YTD        )
                        .replace("#PE#"                ,PE               )
                        .replace("#PE_YTD#"            ,PE_YTD           )
                        .replace("#TPDT#"              ,TPDT             )
                        .replace("#TPDT_YTD#"          ,TPDT_YTD         )
                        .replace("#TE#"                ,TE               )
                        .replace("#TE_YTD#"            ,TE_YTD           )
                        .replace("#usage#"             ,usage            )
                        .replace("#usage_YTD#"         ,usage_YTD        )
                        .replace("#change_over#"       ,change_over      )
                        .replace("#change_over_YTD#"   ,change_over_YTD  ));
                }

            }

            if(pages === 1){
                var htmlHeader = new StringBuilder();
                htmlHeader.append("<div class='w20 strong-label'>Line "+line+"</div>");
                htmlHeader.append("<table class='w300'>");
                htmlHeader.append(_self.reportData.resultHeader);
                htmlHeader.append(htmlValue.toString());
                htmlHeader.append(htmlTotal.toString());
                htmlHeader.append("</table>");
                $("#reportHolder").html(_self.reportData.mainHeader);
                $(".mainTable").html(htmlHeader.toString());
                $(".A4.p_" + pages).addClass(orientation);
            }

            _self.updateMainHeader(params,(pages-1));
            _self.reportData = {};
            _self.callBack(callback);
        });
    };
    _self.buildPackagingChangeoversReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = _self.prepareMainHeader("packaging - technical changeovers");

        _self.reportData.resultHeader = "<tr class='heading'>\n\
                                            <td class='w10'>Date</td>\n\
                                            <td class='w5'>Shift</td>\n\
                                            <td class='w10'>From</td>\n\
                                            <td class='w10'>To</td>\n\
                                            <td class='w40'>Change over time [min]</td>\n\
                                        </tr>";
        
        _self.reportData.rowDefinition = "<tr>\n\
                                            <td class='w10'>#date#</td>\n\
                                            <td class='w5'>#shift#</td>\n\
                                            <td class='w10'>#product_id_from#</td>\n\
                                            <td class='w10'>#product_id_to#</td>\n\
                                            <td class='w40'>#minutes#</td>\n\
                                        </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {

            var count = result.length;
            var html = new StringBuilder();
            var perPage = 35;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "portrait";

            var line = 0;
            var previousLine = 0;

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;

                line = result[i].line_id;

                if(previousLine == 0)
                    previousLine = line;

                if(line != previousLine || resultTracker == perPage){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<div class='w20 strong-label'>Line "+previousLine+"</div>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").html(htmlHeader.toString());
                        $(".A4.p_" + pages).addClass(orientation);
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<div class='w20 strong-label'>Line "+previousLine+"</div>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }

                    html.clear();
                    pages = pages + 1;

                }
                previousLine = line;

                resultTracker = resultTracker + 1;

                var date             = (result[i].date               !== undefined)  ? result[i].date:                "";
                var shift            = (result[i].shift              !== undefined)  ? result[i].shift:               "";
                var product_id_from  = (result[i].product_id_from    !== undefined)  ? result[i].product_id_from:     "";
                var product_id_to    = (result[i].product_id_to      !== undefined)  ? result[i].product_id_to:       "";
                var minutes          = (result[i].minutes            !== undefined)  ? result[i].minutes:             "";


                html.append(
                    _self.reportData.rowDefinition.replace("#date#" , date)
                    .replace("#shift#"              , shift             )
                    .replace("#product_id_from#"    , product_id_from   )
                    .replace("#product_id_to#"      , product_id_to     )
                    .replace("#minutes#"            , minutes           ));

            }
            if(pages === 1){
                var htmlHeader = new StringBuilder();
                htmlHeader.append("<div class='column w100 mainTable'>");
                htmlHeader.append("<div class='w20 strong-label'>Line "+previousLine+"</div>");
                htmlHeader.append("<table class='w100'>");
                htmlHeader.append(_self.reportData.resultHeader);
                htmlHeader.append(html.toString());
                htmlHeader.append("</table>");
                htmlHeader.append("</div>");
                $("#reportHolder").html(_self.reportData.mainHeader);
                $(".mainTable").html(htmlHeader.toString());
                $(".A4.p_" + pages).addClass(orientation);
            }

            _self.updateMainHeader(params,(pages-1));
            _self.reportData = {};
            _self.callBack(callback);
        });
    };
    _self.buildPackagingDowntimeReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = "<div class='A4 p_1 portrait'>\n\
                                        <div class='column w100'>\n\
                                            <h1>packaging - downtime</h1>    \n\
                                        </div>\n\
                                        <div class='column w25'>\n\
                                            <table class='header-data w100'>\n\
                                                <tbody><tr>\n\
                                                    <td class='w25 left'>For</td>\n\
                                                    <td class='w75' id='reportForDate'></td>\n\
                                                </tr>\n\
                                                <tr>\n\
                                                    <td class='w25 left'>Entries</td>\n\
                                                    <td class='w75'>---</td>\n\
                                                </tr>\n\
                                                <tr>\n\
                                                    <td class='w25 left'>User</td>\n\
                                                    <td class='w75' id='reportUser'></td>\n\
                                                </tr>\n\
                                                <tr>\n\
                                                    <td class='w25 left'>Data</td>\n\
                                                    <td class='w75' id='reportTime'></td>\n\
                                                </tr>\n\
                                                <tr>\n\
                                                    <td class='w25 left'>Pages</td>\n\
                                                    <td class='w75' id='reportPage'></td>\n\
                                                </tr>\n\
                                            </tbody></table>\n\
                                        </div>\n\
                                        <div class='column w75'>\n\
                                            <div class='column w100 np' style='height: 95px;'>\n\
                                                <hr>\n\
                                                <div class='column np w25'>\n\
                                                    <span>Notes</span>\n\
                                                </div>\n\
                                                <div class='column np w75 text-right'>\n\
                                                    <span>Dr. Oetker Canada Ltd.</span>\n\
                                                </div>\n\
                                            </div>\n\
                                        </div>\n\
                                        <div class='column w100 np'>\n\
                                            <div class='column w25'>\n\
                                                <hr>\n\
                                            </div>\n\
                                            <div class='column w75'>\n\
                                                <div class='note'><strong>Notes:</strong> downtime codes percentages are with respect to total down time </div>\n\
                                                <hr>    \n\
                                            </div>\n\
                                        </div>\n\
                                        <div class='column w100 mainTable'>\n\
                                        </div>\n\
                                    </div>";

        _self.reportData.resultHeader = "<tr class='heading'>\n\
                                            <td class='w5'><div class='rotate'>Line</div></td>\n\
                                            <td class='w5'><div class='rotate'>Shift</div></td>\n\
                                            <td class='w5'><div class='rotate'>Speed</div></td>\n\
                                            <td class='w5'><div class='rotate'>Time</div></td>\n\
                                            <td class='w5'>#Item</td>\n\
                                            <td class='w30 text-left'>Description</td>\n\
                                            <td class='w5'>Roll Code [%]</td>\n\
                                            <td class='w5'>No mix [%]</td>\n\
                                            <td class='w5'>Un. Acc. [%]</td>\n\
                                            <td class='w5'>Fill Mach. [%]</td>\n\
                                            <td class='w5'>Cart. Wrap. [%]</td>\n\
                                            <td class='w5'>Check. Weigh. [%]</td>\n\
                                            <td class='w5'>Case Packer [%]</td>\n\
                                            <td class='w5'>Total [min]</td>\n\
                                            <td class='w5'>Total [%]</td>\n\
                                        </tr>";
        
        _self.reportData.rowDefinition = "<tr>\n\
                                            <td>#line#</td>\n\
                                            <td>#shift#</td>\n\
                                            <td>#speed#</td>\n\
                                            <td>#minutes#</td>\n\
                                            <td>#item#</td>\n\
                                            <td class='text-left'>#description#</td>\n\
                                            <td>#rollCodePerc#</td>\n\
                                            <td>#noMixPerc#</td>\n\
                                            <td>#unAccPerc#</td>\n\
                                            <td>#fillMachinePerc#</td>\n\
                                            <td>#cartWrappPerc#</td>\n\
                                            <td>#checkWeighterPerc#</td>\n\
                                            <td>#casePackerPerc#</td>\n\
                                            <td>#totalDowntime#</td>\n\
                                            <td>#totalDowntimePerc#</td>\n\
                                        </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {
            var count = result.length;
            var html = new StringBuilder();
            var perPage = 20;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "portrait";

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;
                
                resultTracker = resultTracker + 1;
                var line               = (result[i].line              !== undefined)  ? result[i].line               : "";
                var shift              = (result[i].shift             !== undefined)  ? result[i].shift              : "";
                var speed              = (result[i].speed             !== undefined)  ? result[i].speed              : "";
                var minutes            = (result[i].minutes           !== undefined)  ? result[i].minutes            : "";
                var item               = (result[i].item              !== undefined)  ? result[i].item               : "";
                var description        = (result[i].description       !== undefined)  ? result[i].description        : "";
                var rollCodePerc       = (result[i].rollCodePerc      !== undefined)  ? result[i].rollCodePerc       : "";
                var noMixPerc          = (result[i].noMixPerc         !== undefined)  ? result[i].noMixPerc          : "";
                var unAccPerc          = (result[i].unAccPerc         !== undefined)  ? result[i].unAccPerc          : "";
                var fillMachinePerc    = (result[i].fillMachinePerc   !== undefined)  ? result[i].fillMachinePerc    : "";
                var cartWrappPerc      = (result[i].cartWrappPerc     !== undefined)  ? result[i].cartWrappPerc      : "";
                var checkWeighterPerc  = (result[i].checkWeighterPerc !== undefined)  ? result[i].checkWeighterPerc  : "";
                var casePackerPerc     = (result[i].casePackerPerc    !== undefined)  ? result[i].casePackerPerc     : "";
                var totalDowntime      = (result[i].totalDowntime     !== undefined)  ? result[i].totalDowntime      : "";
                var totalDowntimePerc  = (result[i].totalDowntimePerc !== undefined)  ? result[i].totalDowntimePerc  : "";


                html.append(
                    _self.reportData.rowDefinition
                    .replace("#line#"              ,line               )
                    .replace("#shift#"             ,shift              )
                    .replace("#speed#"             ,speed              )
                    .replace("#minutes#"           ,minutes            )
                    .replace("#item#"              ,item               )
                    .replace("#description#"       ,description        )
                    .replace("#rollCodePerc#"      ,rollCodePerc       )
                    .replace("#noMixPerc#"         ,noMixPerc          )
                    .replace("#unAccPerc#"         ,unAccPerc          )
                    .replace("#fillMachinePerc#"   ,fillMachinePerc    )
                    .replace("#cartWrappPerc#"     ,cartWrappPerc      )
                    .replace("#checkWeighterPerc#" ,checkWeighterPerc  )
                    .replace("#casePackerPerc#"    ,casePackerPerc     )
                    .replace("#totalDowntime#"     ,totalDowntime      )
                    .replace("#totalDowntimePerc#" ,totalDowntimePerc  ));

                if(resultTracker == perPage){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){                        
                        htmlHeader.append("<table class='w300'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").html(htmlHeader.toString());
                        $(".A4.p_" + pages).addClass(orientation);
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append("<table class='w100'>");
                        htmlHeader.append(_self.reportData.resultHeader);
                        htmlHeader.append(html.toString());
                        htmlHeader.append("</table>");
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }

                    html.clear();
                    pages = pages + 1;

                }
            }

            if(pages === 1){
                var htmlHeader = new StringBuilder();
              
                htmlHeader.append("<table class='w300'>");
                htmlHeader.append(_self.reportData.resultHeader);
                htmlHeader.append(html.toString());
                htmlHeader.append("</table>");
                $("#reportHolder").html(_self.reportData.mainHeader);
                $(".mainTable").html(htmlHeader.toString());
                $(".A4.p_" + pages).addClass(orientation);
            }

            _self.updateMainHeader(params,(pages-1));
            _self.reportData = {};
            _self.callBack(callback);
        });
    };

    _self.buildPackagingSilosReport = function(params,callback) {

        $("#reportHolder").empty();

        _self.reportData.mainHeader = _self.prepareMainHeader("silo levels");

        _self.reportData.resultHeader = "<tr class='heading2'>\n\
                                            <td class='w20'>Silo</td>\n\
                                            <td class='w20'>Start day</td>\n\
                                            <td class='w20'>End day</td>\n\
                                            <td class='w20'>Usage</td>\n\
                                            <td class='w20'>Delivered</td>\n\
                                        </tr>";

        _self.reportData.rowDefinition = "<tr>\n\
                                            <td class='w20'>#Silo#</td>\n\
                                            <td class='w20'>#startDay#</td>\n\
                                            <td class='w20'>#endDay#</td>\n\
                                            <td class='w20'>#Usage#</td>\n\
                                            <td class='w20'>#Delivered#</td>\n\
                                        </tr>";

        window.backendInterface.getData(window.CONSTANTS.SERVICE_URL, params, function(result) {

            var count = result.length;
            var html = new StringBuilder();
            var htmlPerPage = new StringBuilder();
            var perPage = 4;
            var resultTracker = 0;
            var pages = 1;
            var orientation = "portrait";

            var date = "";
            var previousDate = "";

            for (var i = 0; i < count; i++) {
                if(!result[i]) break;

                date = result[i].Date;

                if(previousDate == "")
                    previousDate = date;

                if(date != previousDate || (i == count-1)){
                    htmlPerPage.append("<div class='column np w100 mainTable packagingDaily'>");
                    htmlPerPage.append("<div class='column w25'><div class='strong-label w100'>"+previousDate+"</div></div>");
                    htmlPerPage.append("<div class='column w75'>");
                    htmlPerPage.append("<table class='w100'>");
                    htmlPerPage.append(_self.reportData.resultHeader);
                    htmlPerPage.append(html.toString());
                    htmlPerPage.append("</table>");
                    htmlPerPage.append("</div></div>");

                    html.clear();

                    resultTracker = resultTracker + 1;
                }

                if(resultTracker === perPage || (i == count-1)){
                    resultTracker = 0;

                    var htmlHeader = new StringBuilder();

                    if(pages === 1){
                        $("#reportHolder").html(_self.reportData.mainHeader);
                        $(".mainTable").replaceWith(htmlPerPage.toString());
                        $(".A4.p_" + pages).addClass(orientation);

                        htmlPerPage.clear();
                    }
                    else{
                        htmlHeader.append("<div class='column w100 mainTable'>");
                        htmlHeader.append(htmlPerPage.toString());
                        htmlHeader.append("</div>");
                        _self.createPage(pages,orientation);
                        $(".A4.p_"+pages).html(htmlHeader.toString());
                    }
                    htmlPerPage.clear();
                    pages = pages + 1;
                }

                previousDate = date;

                var Silo        = (result[i].Silo      !== undefined)  ? result[i].Silo:      "";
                var startDay    = (result[i].startDay  !== undefined)  ? result[i].startDay:  "";
                var endDay      = (result[i].endDay    !== undefined)  ? result[i].endDay:    "";
                var Usage       = (result[i].Usage     !== undefined)  ? result[i].Usage:     "";
                var Delivered   = (result[i].Delivered !== undefined)  ? result[i].Delivered: "";

                html.append(
                    _self.reportData.rowDefinition.replace("#Silo#"  ,Silo)
                    .replace("#startDay#"   ,startDay       )
                    .replace("#endDay#"     ,endDay         )
                    .replace("#Usage#"      ,Usage          )
                    .replace("#Delivered#"  ,Delivered      ));

            }

            if(html.toString() !== ""){
                    htmlPerPage.append("<div class='column np w100 mainTable packagingDaily'>");
                    htmlPerPage.append("<div class='column w25'><div class='strong-label w100'>"+previousDate+"</div></div>");
                    htmlPerPage.append("<div class='column w75'>");
                    htmlPerPage.append("<table class='w100'>");
                    htmlPerPage.append(_self.reportData.resultHeader);
                    htmlPerPage.append(html.toString());
                    htmlPerPage.append("</table>");
                    htmlPerPage.append("</div></div>");

                    html.clear();
            }

            if(pages === 1){
                var htmlHeader = new StringBuilder();
                htmlHeader.append(htmlPerPage.toString());
                $("#reportHolder").html(_self.reportData.mainHeader);
                $(".mainTable").replaceWith(htmlHeader.toString());
                $(".A4.p_" + pages).addClass(orientation);

                htmlPerPage.clear();
            }

            _self.updateMainHeader(params,(pages-1));
            _self.reportData = {};
            _self.callBack(callback);
        });
    };

    _self.printReport = function() {

            var html = new StringBuilder();

            html.append("<html>");
            html.append("<head>");
            html.append($("head").html());
            html.append("</head>");
            html.append("<body onload=\"window.print()\">");
            html.append($("#reportHolder").html());
            html.append("</body>");
            html.append("</html>");
            var popupWin = window.open();
            popupWin.document.open();
            popupWin.document.write(html.toString());
            //popupWin.document.close();
    };
};